<?php
/* @var $this CriteriaController */
/* @var $model Criteria */
/* @var $form CActiveForm */
?>
<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'criteria-form',
	'enableAjaxValidation'=>false,
)); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php echo $form->errorSummary($model); ?>

	<div class="row">
		<?php echo $form->labelEx($model,'prop_name'); ?>
		<?php
			$properties = Properties::model()->fetchProperties(Yii::app()->user->getState('client_id'));
			//echo $form->dropDownList($model, 'prop_name', CHtml::listData($properties, 'prop_id', 'prop_name'));
			$dropdown = '<select id="Criteria_prop_name" name="Criteria[prop_name]">';
			foreach ($properties as $property){
				$dropdown = $dropdown.'<option value="'.$property[0].'" datatype="'.$property[1].'" fromcontact="'.$property[2].
							'" fromlog="'.$property[3].'">'.$property[0].'</option>';
			}
			echo $dropdown."</select>";
		?>
		<?php echo $form->error($model,'prop_name'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'operator');
			$ops = StaticLookup::model()->loadLookup("Operators");
			echo $form->dropDownList($model, 'operator', CHtml::listData($ops, 'default_val', 'lu_val')); 
			//echo $form->textField($model,'operator',array('size'=>60,'maxlength'=>200));
			echo $form->error($model,'operator'); 
		?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'prop_val'); ?>
		<?php echo $form->textField($model,'prop_val',array('size'=>60,'maxlength'=>100)); ?>
		<?php echo $form->error($model,'prop_val'); ?>
	</div>

	<div class="row">
		<?php echo $form->hiddenField($model,'list_id',array('size'=>10,'maxlength'=>10)); ?>
		<?php echo $form->hiddenField($model,'fromLogentry',array('size'=>10,'maxlength'=>10)); ?>
	</div>

	<div class="clear-fix"></div>
	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->
<script>
		$(document).ready(function(){
			$("#Criteria_prop_name").change(function(){
				$('#Criteria_prop_val').datetimepicker('destroy');
				$('#Criteria_prop_val').unbind("click");
				var selProp = $("#Criteria_prop_name").val();
				var selPropType = $("#Criteria_prop_name > option[value='"+selProp+"']").attr("datatype");
				var selPropfromlog = $("#Criteria_prop_name > option[value='"+selProp+"']").attr("fromlog");
				var selPropfromcontact = $("#Criteria_prop_name > option[value='"+selProp+"']").attr("fromcontact");
				loadOperators(selPropType, 'Criteria_operator');
				loadEditor(selPropType, 'Criteria_prop_val');
				if(selPropfromlog.toLowerCase() === "false" && selPropfromcontact.toLowerCase() === "false"){
					$("#Criteria_fromLogentry").val(0);
				}
				else if(selPropfromlog.toLowerCase() === "true"){
					$("#Criteria_fromLogentry").val(1);
				}
				else if(selPropfromcontact.toLowerCase() === "true"){
					$("#Criteria_fromLogentry").val(2);
				}
			});

			$("#Criteria_prop_name").trigger("change");
		});
</script>